package com.addcn.texas.view.components
{
	import flash.display.*;
	import flash.events.*;	
	import flash.text.TextField;
	import flash.system.ApplicationDomain;
	import com.addcn.common.utils.AppLibary
	
	
	public class LoginModule extends MovieClip
	{	
		public static const EVENT = "loginEvent";
		private var _photo_obj : Sprite;
		
		public function LoginModule() {				
			showLoginForm();
			this.loginForm.senter.useHandCursor = true;
			this.loginForm.senter.addEventListener(MouseEvent.MOUSE_DOWN, loginSendBtnClickHandler);			
			this.loginForm.account.addEventListener(KeyboardEvent.KEY_DOWN,keyPressHandle);
			this.loginForm.pwd.addEventListener(KeyboardEvent.KEY_DOWN,keyPressHandle);
		}
		
		private function loginSendBtnClickHandler(evt:Event) {			
			if (this.loginForm.senter.enabled) {
				var _class = AppLibary.getInstance().getClass("com.addcn.common.event.ParamEvent");
				var _account = this.loginForm.account.text.replace(/^\s*/g, "");
			    _account = _account.replace(/\s*$/g, "");	
				var loginEvent = new _class(EVENT,{account:_account,pwd:this.loginForm.pwd.text});
				dispatchEvent(loginEvent);							
			}					
		}
						
		public function btnEnabled() {
			this.loginForm.account.enabled = true;
			this.loginForm.pwd.enabled = true;
			this.loginForm.senter.enabled = true;
			this.loginForm.senter.useHandCursor = true;		
			resetPwd();
			this.stage.focus = this.loginForm.account;
		}
		
		public function btnDisabled() : void
		{
			this.stage.focus = null;
			this.loginForm.account.enabled = false;
			this.loginForm.pwd.enabled = false;
			this.loginForm.senter.enabled = false;
			this.loginForm.senter.useHandCursor = false;			
		}
			
		public function  resetPwd() {
			this.loginForm.pwd.text = "";
		}		
		
		
		public function showUserInfo(r_nick : String ,r_gold : String ) : void 
		{			  
			this._photo_obj = new Sprite;
			this._photo_obj.x = 0;
			this._photo_obj.y = 0;
			this.UserInfoBox.visible = true;
			this.UserInfoBox.NickName.text = r_nick;
			this.UserInfoBox.GameGold.text = r_gold;
			this.UserInfoBox.UserPhoto.addChild(this._photo_obj);
			this.removeChild(this.loginForm);			
		}
		
		public function getPhotoObj() : Sprite 
		{			
			return this._photo_obj;
		}
		
		public function setUserPhoto() : void {
			
		}
		
		public function showLoginForm() : void
		{
			resetPwd();
			this.UserInfoBox.visible = false;
			this.loginForm.visible = true;
			
		}
		
		private  function keyPressHandle(evt : KeyboardEvent) {
			if (evt.keyCode == 13) {
				if (evt.currentTarget.name == "account") {
					this.stage.focus = this.loginForm.pwd;
				}else if (evt.currentTarget.name == "pwd") {
						loginSendBtnClickHandler(new Event(MouseEvent.CLICK));
				}
			}			
		}
		
	}
}
